//
//  FlipsideViewController.m
//  SaveMoney
//
//  Created by Jose Sánchez Martínez on 11/11/11.
//  Copyright (c) 2011 Mubiquo. All rights reserved.
//

#import "FlipsideViewController.h"

@implementation FlipsideViewController

@synthesize delegate = _delegate;

NSArray *themeArray;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [objetivo becomeFirstResponder];
    themeArray = [[NSArray arrayWithObjects:@"Gadget", @"Vehicle",@"Viatge", @"Altres", nil] retain];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (IBAction) changeDatePicker {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-yyyy"];
    NSString *stringFromDate = [formatter stringFromDate:datePicker.date];
    [formatter release];
    [fecha setText:stringFromDate];
}


#pragma mark - Actions

- (IBAction)done:(id)sender
{
    [self.delegate flipsideViewControllerDidFinish:self];
}


#pragma mark DELEGATES


#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == objetivo) {
        
        [UIView animateWithDuration:0.3 
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             CGRect rect = datePicker.frame;
                             rect.origin.y = self.view.bounds.size.height;
                             datePicker.frame = rect;
                             rect = pickerView.frame;
                             rect.origin.y = self.view.bounds.size.height;
                             pickerView.frame = rect;
                         }
                         completion:nil];
        return YES;
    }
    else if (textField == fecha) {
        [self changeDatePicker];
        [objetivo resignFirstResponder];
        
        [UIView animateWithDuration:0.3 
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             CGRect rect = datePicker.frame;
                             rect.origin.y = self.view.bounds.size.height - datePicker.frame.size.height;
                             datePicker.frame = rect;
                             rect = pickerView.frame;
                             rect.origin.y = self.view.bounds.size.height;
                             pickerView.frame = rect;
                         }
                         completion:nil]; 
    }
    else if (textField == tema) {
        tema.text = [themeArray objectAtIndex:0];
        [objetivo resignFirstResponder];
        [UIView animateWithDuration:0.3 
                              delay:0
                            options:UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             CGRect rect = pickerView.frame;
                             rect.origin.y = self.view.bounds.size.height - pickerView.frame.size.height;
                             pickerView.frame = rect;
                             rect = pickerView.frame;
                             rect.origin.y = self.view.bounds.size.height;
                             datePicker.frame = rect;
                         }
                         completion:nil];
    }
    return NO;
}

#pragma mark UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [themeArray count];
}

#pragma mark UIPickerViewDelegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:row forKey:@"theme"];
    tema.text = [themeArray objectAtIndex:row];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [themeArray objectAtIndex:row];
}

@end
